@extends("layouts.backend.webadmin")
@section('page-header')
<h1>
    <small>orders</small>
</h1>
@endsection

@section('content')
<link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/jquery-treetable/css/jquery.treetable.css">
<link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/select2/css/select2.min.css">
<link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/select2/css/select2totree.css">
<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <!-- /.box-header -->
                <div class="box-body">
                    <table id="order-list" class="table table-bordered table-striped">
                        <thead>
                        <tr>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</section>
<input type="hidden" name="status" value="{{$status}}" />
@stop
@section('javascript-end')
<script src="{{env('APP_ROOT')}}/assets/layui/layui.all.js"></script>
<script src="{{env('APP_ROOT')}}/assets/select2/js/select2.min.js"></script>
<script src="{{env('APP_ROOT')}}/assets/select2/js/select2totree.js"></script>
<script src="{{env('APP_ROOT')}}/assets/select2/js/i18n/zh-CN.js"></script>
<script src="{{env('APP_ROOT')}}/js/common.js"></script>
<script src="{{env('APP_ROOT')}}/js/jquery.jeditable.mini.js"></script>
<script>
    $(function () {
        var status = $.parseJSON($('input[name="status"]').val());
        var m1 = manage();
        var tables = $('#order-list').DataTable({
            //调整结构
            "dom": '<"dataTables_filter">rtip',
            "autoWidth":true,
            //国际化
            'oLanguage'   :{
                'sUrl'    : '{{env("APP_ROOT")}}/assets/bootstrap/js/zh_cn.json'
            },
            "initComplete": function( settings, json ) {

                var _$this = this;
                var button = '<button class="btn btn-primary recyle-list orders-index" style="float: left;">回收站</button>';
                $('.dataTables_filter').html(button);

                var _select = '<label><span>状态:</span> <select class="form-control input-sm">'+ getOption(status) +'</select>';

                m1.filter(jQuery,_select,_$this,false);
            },
            "paging": true,
            "ordering": true,
            "processing": true,
            //开启服务器模式
            "serverSide": true,
            //默认排序
            "order":[7,'desc'],
            ajax: {
                //指定数据源
                url: "{{route('admin.order.search')}}",
                type: "GET",
                data: function(d){

                    return userManage.getQueryCondition(d);
                },
                //接受服务器数据
                dataSrc:function(json){
                    getToken();
                    return json.data;
                }
            },
            //data值对应数据库字段名
            columns: [
                {
                    'title':'订单号',
                    "data": "sn"
                },
                {
                    'title':'商品名称',
                    "data": "goods_name"
                },
                {
                    'title':'商品数量',
                    "data": "goods_num"
                },
                {
                    'title':'兑换人',
                    // "data": "user_name",
                    "render": function(data,type,row,meta){

                        return row.real_name + '(' + row.user_name + ')';
                    }

                },
                {
                    'title':'收货地址',
                    "data": "consignee",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        @permission('order-update')
                        $(nTd).addClass('inputTd').attr('id', oData.id );
                        @endauth
                    }
                },
                {
                    'title':'支付额',
                    "data": "payed"
                },
                {
                    "title":"状态",
                    "data": "status",
                    "render": function(data,type,row,meta){
                        //data 当前项值row 当前列值
                        for(var i = 0;i<status.length;i++){
                            if(data==i){
                                return status[i];
                            }
                        }
                    },
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        @permission('order-update')
                        $(nTd).addClass('selectTd').attr('id', oData.id);
                        @endauth
                    }
                },
                {
                    "title":"下单时间",
                    "data": "created_at"
                },
                {
                    "title":"更新时间",
                    "data": "updated_at"
                },
                {
                    "title":"操作",
                    "targets": -2,
                    "data": null,
                    "render": function(data,type,row,meta){
                        var button = m1.button('order');
                        if(row.deleted_at != null){
                            button1 = button[2] + button[3];
                        }else{
                            button1 = button[1];
                        }
                        return button1;
                    }
                }],
            //所有列过滤xss
            columnDefs: [{
                "targets": "_all",
                "render": $.fn.dataTable.render.text()
            },{
                "targets": [0,1,3,4,6,9],
                "orderable":false
            }],
            "fnDrawCallback": function (data, x) {
                $('#order-list tbody td.inputTd').editable("{{route('admin.order.orderupdate')}}",
                    getEditOption('consignee','input')
                );
                $('#order-list tbody td.selectTd').editable("{{route('admin.order.orderupdate')}}",

                    getEditOption('status','select',status)
                );
            }
        });
        //软删除
        $('#order-list tbody').on( 'click', 'button#delrow', function () {
            var data = tables.row($(this).parents('tr') ).data();
            m1.delete(jQuery,layer,"{{route('admin.order.orderdel')}}",data,tables);
        });
        //启用
        $('#order-list tbody').on( 'click', 'button.activerow', function () {
            var data = tables.row($(this).parents('tr') ).data();
            m1.active(jQuery,layer,"{{route('admin.order.orderrestore')}}",data,tables);
        });
        //强制删除
        $('#order-list tbody').on( 'click', 'button.forcedelrow', function () {
            var data = tables.row($(this).parents('tr') ).data();
            m1.active(jQuery,layer,"{{route('admin.order.orderforcedel')}}",data,tables);
        });
    });
    //自定义请求条件
    var userManage = {
        getQueryCondition : function (data){
            var param = {};
            //组装排序参数
            if (data.order&&data.order.length&&data.order[0]) {
                switch (data.order[0].column) {
                    case 7:
                        param.orderColumn = "created_at";
                        break;
                    case 2:
                        param.orderColumn = "num";
                        break;
                    case 5:
                        param.orderColumn = "payed";
                        break;
                    case 8:
                        param.orderColumn = "updated_at";
                        break;
                    default:
                        param.orderColumn = "created_at";
                        break;
                }
                //排序方式asc或者desc
                param.orderDir = data.order[0].dir;
            }
            var select = data.search.value.split('&');
            param.status = select[0]>=0 ? select[0] : '';
            param.keyworld = select[1] ? select[1] : '';
            param.deleted_at = select[2] ? select[2].split('=')[1] : 0;
            //组装分页参数
            param.start = data.start;
            param.length = data.length;
            param.draw = data.draw;
            return param;
        }
    };
    //自定义select option
    function getOption (val){
        var str = '<option value="">请选择</option>';
        for(var i=0;i < val.length;i++){
            str += '<option value="'+i+'">'+val[i]+'</option>';
        }
        return str;
    }
    //自定义edit
    function getEditOption(column, type,data) {
        var obj = {
            id: 'id',
            name: column,
            submitdata: {column: column},
            indicator: '保存中...',
            tooltip: '点击编辑',
            onblur: 'cancel',
            submit: '确定',
            callback: function (value, settings) {
                value = $.parseJSON(value);
                if (value.code !== 0) {
                    layer.msg(value.msg);
                } else {
                    $(this).html(value.data);
                }
            }
        };
        if(type == 'select'){
            $.extend(obj,{'data':data,'type':'select'});

        }
        return obj;
    }
</script>
@endsection
